﻿using AutoMan.Models;
using AutoMan.Pages.Componets;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraLayout;
using DevExpress.XtraPrinting.Export;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AutoMan.Helper
{
    public class ControlsHelper
    {
        readonly SqlSugar.SqlSugarScope db = SqlsugarHelper.Db;
        private FormModel forminfo;
        private List<FormFieldModel> formfieldinfos;
        private EditDialog editDialog;
        public ControlsHelper(string tableName, EditDialog editDialog)
        {
            this.editDialog = editDialog;
            forminfo = db.Queryable<FormModel>().First(it => it.别名 == tableName);
            formfieldinfos = db.Queryable<FormFieldModel>().Where(it => it.表单编号 == forminfo.编号&&it.名称!="编号").ToList();
        }

        public void InitForm()
        {
            var len = formfieldinfos.Count();
            var items = new LayoutControlItem[len];
            var height = 45;
            Control control=new Control();
            for (int i = 0; i < len; i++)
            {

                switch (formfieldinfos[i].类型)
                {
                    case "TextEdit":
                        control = CreateTextEditControl(i);
                        break;
                    default:
                        break;
                }
                // 创建一个新的 LayoutControlItem 来容纳 TextEdit
                items[i] = new LayoutControlItem();
                if (i % 2 == 0)
                {
                    height = i * 45;
                    items[i].Location = new Point(0, height);
                    items[i].Size = new Size(520, 560);
                }
                else
                {
                    items[i].Location = new Point(520, height);
                    items[i].Size = new Size(520, 560);
                }

                items[i].Text = $"{formfieldinfos[i].名称}:";
                items[i].TextVisible = true;
                items[i].Control = control;
                // 设置控件为必填项（可选）
                // items[i].Required = DefaultBoolean.True;
            }
            editDialog.layoutControl1.Root.Items.Clear(); // 清除任何现有的项目
            editDialog.layoutControl1.Root.AddRange(items);
        }
        public LayoutControlItem CreateLayoutControlItem(Point point, Size size, string text, int i)
        {
            LayoutControlItem item = new LayoutControlItem();
            item.Location = point;
            item.Name = $"item{i}";
            item.Size = size;
            item.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;
            item.TextSize = new Size(0, 0);
            item.Text = $"{text}：";
            item.TextVisible = true;
            return item;
        }
        public DevExpress.XtraEditors.TextEdit CreateTextEditControl(int i)
        {
            TextEdit textEdit = new TextEdit();

            // 设置 TextEdit 的属性（如需要）
            textEdit.Properties.AppearanceDisabled.Options.UseTextOptions = true;
            textEdit.Properties.AppearanceDisabled.TextOptions.HAlignment = HorzAlignment.Center;
            textEdit.Name = $"textEdit{i + 1}";
            return textEdit;
        }
        public DevExpress.XtraEditors.MemoEdit CreateMemoEditControl(int i)
        {
            DevExpress.XtraEditors.MemoEdit memoEdit = new DevExpress.XtraEditors.MemoEdit();
            memoEdit.Location = new Point(129, 335);
            memoEdit.Name = $"memoEdit{i}";
            memoEdit.Size = new Size(779, 222);
            memoEdit.StyleController = editDialog.layoutControl1;
            memoEdit.TabIndex = i;
            return memoEdit;
        }
        public void CreateListControl()
        {

        }
        public void CreateImageControl()
        {

        }
    }
}
